package com.hjq.mallmember.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hjq.common.exception.ExceptionEnum;
import com.hjq.mallmember.entity.vo.UserLoginVO;
import com.hjq.mallmember.entity.vo.UserRegVO;
import com.hjq.mallmember.exception.phoneExistException;
import com.hjq.mallmember.exception.userNameExistException;
import com.hjq.mallmember.feign.CouponRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hjq.mallmember.entity.MemberEntity;
import com.hjq.mallmember.service.MemberService;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.R;



/**
 * 会员
 *
 * @author Hjq
 * @email 1484397309@qq.com.com
 * @date 2022-05-24 16:57:34
 */
@RestController
@RequestMapping("mallmember/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponRemoteService couponRemoteService;

    @PostMapping("/login")
    //@RequiresPermissions("mallmember:member:list")
    public R login(@RequestBody UserLoginVO loginVO){
      MemberEntity memberEntity=  memberService.login(loginVO);
      if(memberEntity==null){
         return R.error(ExceptionEnum.LOGIN_EXIST_EXCEPTION.getNumber(),ExceptionEnum.LOGIN_EXIST_EXCEPTION.getMsg());
      }
      return R.ok().put("account",memberEntity);
    }
    @PostMapping("/register")
    //@RequiresPermissions("mallmember:member:list")
    public R register(@RequestBody UserRegVO regVO){
        try {
            memberService.registerNewMember(regVO);
        } catch (phoneExistException e1) {
            e1.printStackTrace();
            return R.error(ExceptionEnum.PHONE_EXIST_EXCEPTION.getNumber(),ExceptionEnum.PHONE_EXIST_EXCEPTION.getMsg());

        } catch (userNameExistException e2) {
            return R.error(ExceptionEnum.USERNAME_EXIST_EXCEPTION.getNumber(),ExceptionEnum.USERNAME_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }
    /**
     * 获取所有会员等级
     */
//    @RequestMapping("/memberlevel/list")
//    //@RequiresPermissions("mallmember:member:list")
//    public R memberLevelList(@RequestParam Map<String, Object> params){
//        PageUtils page = memberService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }


    //测试feign
    @RequestMapping("/coupon")
    //@RequiresPermissions("mallmember:member:list")
    public R testFeign(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setUsername("FUck");
        R memberList = couponRemoteService.memberList();
        Object couponEntity = memberList.get("couponEntity");
        return R.ok().put("memberEntity",memberEntity).put("coupon",couponEntity);
    }

    /**
     * 列表
     */


    @RequestMapping("/list")
    //@RequiresPermissions("mallmember:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("mallmember:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("mallmember:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("mallmember:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("mallmember:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
